home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 13 - 1997 (partial) / 13.01 Jan 97 / Getting Started, javaDraw / javaDraw.java < prev   
Encoding:
Java Source  |  1996-10-16  |  3.1 KB  |  152 lines  |  [TEXT/CWIE]

  1. import java.awt.*;
  2.  
  3. public class shapeCanvas extends Canvas
  4. {
  5.     final int     rectangle=0,
  6.                     rect3D=1,
  7.                     roundRect=2,
  8.                     arc=3,
  9.                     oval=4,
  10.                     line=5,
  11.                     polygon=6;
  12.     int            whichShape = rectangle;
  13.     boolean        isFilled = true;
  14.     
  15.     shapeCanvas( int width, int height )
  16.     {
  17.         setBackground( Color.yellow );
  18.         setForeground( Color.red );
  19.         resize( width, height );
  20.     }
  21.     
  22.     public void SetShape( int newShape )
  23.     {
  24.         whichShape = newShape;
  25.     }
  26.     
  27.     public void SetIsFilled( boolean newIsFilled )
  28.     {
  29.         isFilled = newIsFilled;
  30.     }
  31.     
  32.     public void paint( Graphics g )
  33.     {
  34.         Rectangle    r, b;
  35.         Polygon        poly;
  36.         
  37.         b = bounds();
  38.         r = new Rectangle( 10, 10, b.width - 20, b.height - 20 );
  39.         
  40.         poly = new Polygon();
  41.         poly.addPoint( r.x, r.y );
  42.         poly.addPoint( r.x, r.y + r.height );
  43.         poly.addPoint( r.x + r.width, r.y + r.height );
  44.         poly.addPoint( r.x, r.y + r.height/2 );
  45.         poly.addPoint( r.x + r.width, r.y + r.height/2 );
  46.         poly.addPoint( r.x, r.y );
  47.         
  48.         if ( isFilled )
  49.         {
  50.             switch ( whichShape )
  51.             {
  52.                 case rectangle:
  53.                     g.fillRect( r.x, r.y, r.width, r.height );
  54.                     break;
  55.                 case rect3D:
  56.                     g.fill3DRect( r.x, r.y, r.width, r.height, true );
  57.                     break;
  58.                 case roundRect:
  59.                     g.fillRoundRect( r.x, r.y, r.width, r.height, 16, 16 );
  60.                     break;
  61.                 case arc:
  62.                     g.fillArc( r.x, r.y, r.width, r.height, 0, 270 );
  63.                     break;
  64.                 case oval:
  65.                     g.fillOval( r.x, r.y, r.width, r.height );
  66.                     break;
  67.                 case line:
  68.                     g.drawLine( r.x, r.y, r.x + r.width, r.y + r.height );
  69.                     break;
  70.                 case polygon:
  71.                     g.fillPolygon( poly );
  72.                     break;
  73.             }
  74.         }
  75.         else
  76.         {
  77.             switch ( whichShape )
  78.             {
  79.                 case rectangle:
  80.                     g.drawRect( r.x, r.y, r.width, r.height );
  81.                     break;
  82.                 case rect3D:
  83.                     g.draw3DRect( r.x, r.y, r.width, r.height, true );
  84.                     break;
  85.                 case roundRect:
  86.                     g.drawRoundRect( r.x, r.y, r.width, r.height, 16, 16 );
  87.                     break;
  88.                 case arc:
  89.                     g.drawArc( r.x, r.y, r.width, r.height, 0, 270 );
  90.                     break;
  91.                 case oval:
  92.                     g.drawOval( r.x, r.y, r.width, r.height );
  93.                     break;
  94.                 case line:
  95.                     g.drawLine( r.x, r.y, r.x + r.width, r.y + r.height );
  96.                     break;
  97.                 case polygon:
  98.                     g.drawPolygon( poly );
  99.                     break;
  100.             }
  101.         }
  102.     }
  103. }
  104.  
  105. public class javaDraw extends java.applet.Applet
  106. {
  107.     private Choice            shapePopup, fillPopup;
  108.     private shapeCanvas    sCanvas;
  109.     
  110.     public void init()
  111.     {
  112.         shapePopup = new Choice();
  113.         shapePopup.addItem( "Rectangle" );
  114.         shapePopup.addItem( "3D Rectangle" );
  115.         shapePopup.addItem( "Rounded Rectangle" );
  116.         shapePopup.addItem( "Arc" );
  117.         shapePopup.addItem( "Oval" );
  118.         shapePopup.addItem( "Line" );
  119.         shapePopup.addItem( "Polygon" );
  120.         
  121.         add( shapePopup );
  122.         
  123.         fillPopup = new Choice();
  124.         fillPopup.addItem( "Fill Shapes" );
  125.         fillPopup.addItem( "Do Not Fill Shapes" );
  126.         
  127.         add( fillPopup );
  128.         
  129.         sCanvas = new shapeCanvas( 208, 110 );
  130.         add( sCanvas );
  131.     }
  132.     
  133.     public boolean action( Event e, Object obj )
  134.     {
  135.         if ( e.target == shapePopup )
  136.         {
  137.             sCanvas.SetShape( shapePopup.getSelectedIndex() );
  138.             sCanvas.repaint();
  139.             
  140.             return true;
  141.         }
  142.         else if ( e.target == fillPopup )
  143.         {
  144.             sCanvas.SetIsFilled( fillPopup.getSelectedIndex() == 0 );
  145.             sCanvas.repaint();
  146.             
  147.             return true;
  148.         }
  149.         else
  150.             return super.action( e, obj );
  151.     }
  152. }